package lambdasinaction.chap1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ThinkPad on 2017/8/15.
 */
public class FilteringApples {

    public static void main(String... args) {
        List<Apple> inventory = Arrays.asList(new Apple(80, ""),
                new Apple(155, "green"),
                new Apple(120, "red"));
        List<Apple> greenApples = filterApples(inventory,FilteringApples::isGreenApple);
        System.out.println(greenApples);
        List<Apple> heavyApples = filterApples(inventory,FilteringApples::isHeavyApple);
        System.out.println(heavyApples);
        List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getWeight()));
        System.out.println(greenApples2);
        List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
        System.out.println(heavyApples2);
        List<Apple> weirApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
                                                                        "brown".equals(a.getColor()));
        System.out.println(weirApples);
    }

    /**
     * 筛选绿苹果
     * @param inventory 待筛选的苹果
     * @return 绿苹果
     */
    public static List<Apple> filterGreenApple(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor())) {
                result.add(apple);
            }
        }
        return result;
    }

    /**
     * 筛选
     * @param inventory 待筛选的苹果
     * @return 重量大于150的苹果
     */
    public static List<Apple> filterHeavyApple(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.getWeight() > 150) {
                result.add(apple);
            }
        }
        return result;
    }

//    java8方法引用做法,把条件代码(方法)作为参数
    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 150;
    }

//    写出来是为了清晰(平常只要从java.util.function导入就可以了)
    public interface Predicate<T> {
        boolean test(T t);
    }

    static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static class Apple {
        private int weight = 0;
        private String color = "";

        public Apple(int weight, String color) {
            this.weight = weight;
            this.color = color;
        }

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        @Override
        public String toString() {
            return "Apple{" +
                    "color='" + color + '\'' +
                    ", weight=" + weight +
                    '}';
        }
    }
}
